package com.blq.api.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blq.api.mapper.EvaluationBackupMapper;
import com.blq.api.service.IEvaluationBackupService;
import com.blq.common.core.domain.PageQuery;
import com.blq.common.core.page.TableDataInfo;
import com.blq.common.utils.StringUtils;
import com.blq.api.domain.EvaluationBackup;
import com.blq.api.domain.Bo.EvaluationBackupBo;
import com.blq.api.domain.Vo.EvaluationBackupVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 评价备份Service业务层处理
 *
 * @author Blq
 * @date 2023-02-01
 */
@RequiredArgsConstructor
@Service
public class EvaluationBackupServiceImpl extends ServiceImpl<EvaluationBackupMapper,EvaluationBackup> implements IEvaluationBackupService {


    private final EvaluationBackupMapper baseMapper;

    /**
     * 查询评价备份
     */
    @Override
    public EvaluationBackupVo queryById(String evaluationId){
        return baseMapper.selectVoById(evaluationId);
    }

    /**
     * 查询评价备份列表
     */
    @Override
    public TableDataInfo<EvaluationBackupVo> queryPageList(EvaluationBackupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EvaluationBackup> lqw = buildQueryWrapper(bo);
        Page<EvaluationBackupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询评价备份列表
     */
    @Override
    public List<EvaluationBackupVo> queryList(EvaluationBackupBo bo) {
        LambdaQueryWrapper<EvaluationBackup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EvaluationBackup> buildQueryWrapper(EvaluationBackupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EvaluationBackup> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationId()), EvaluationBackup::getEvaluationId, bo.getEvaluationId());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationStatus()), EvaluationBackup::getEvaluationStatus, bo.getEvaluationStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationFlag()), EvaluationBackup::getEvaluationFlag, bo.getEvaluationFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationInfo()), EvaluationBackup::getEvaluationInfo, bo.getEvaluationInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialIllustrate()), EvaluationBackup::getMaterialIllustrate, bo.getMaterialIllustrate());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialBefore()), EvaluationBackup::getMaterialBefore, bo.getMaterialBefore());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultCondition()), EvaluationBackup::getFaultCondition, bo.getFaultCondition());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialAfter()), EvaluationBackup::getMaterialAfter, bo.getMaterialAfter());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialPower()), EvaluationBackup::getMaterialPower, bo.getMaterialPower());
        lqw.eq(bo.getCreatTime() != null, EvaluationBackup::getCreatTime, bo.getCreatTime());
        lqw.eq(bo.getMaterialTime() != null, EvaluationBackup::getMaterialTime, bo.getMaterialTime());
        return lqw;
    }

    /**
     * 新增评价备份
     */
    @Override
    public Boolean insertByBo(EvaluationBackupBo bo) {
        EvaluationBackup add = BeanUtil.toBean(bo, EvaluationBackup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEvaluationId(add.getEvaluationId());
        }
        return flag;
    }

    /**
     * 修改评价备份
     */
    @Override
    public Boolean updateByBo(EvaluationBackupBo bo) {
        EvaluationBackup update = BeanUtil.toBean(bo, EvaluationBackup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EvaluationBackup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除评价备份
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
